1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8 package org.webdocwf.util.smime.der;
9
10
11 import org.webdocwf.util.smime.exception.SMIMEException;
12 import org.webdocwf.util.smime.exception.ErrorStorage;
13 import java.util.GregorianCalendar;
14 import java.util.Date;
15 import java.util.SimpleTimeZone;
16 import java.text.SimpleDateFormat;
17
18
19 /***
20 * DERUTCTime is primitive type of DER encoded object which represents Coordinate
21 * Universal Time (or Greenwich Mean Time) in the ASN.1 notation in format
22 * YYMMDDhhmmssZ. For example: 01.02.1971. 11:25:30 GMT+1h take a form in
23 * DERUTCTime object: 710201102530Z.
24 */
25 public class DERUTCTime extends DERObject {
26
27 /***
28 * Constructs UTC time according to user's current local settings (increased
29 * or decreased for time difference to obtain Greenwich Mean Time).
30 * @exception SMIMEException thrown in super class constructor. Also it can be
31 * caused by non SMIMEException which is: UnsupportedEncodingException.
32 */
33 public DERUTCTime() throws SMIMEException {
34 super(23);
35 SimpleTimeZone tz = (SimpleTimeZone) SimpleTimeZone.getDefault();
36
37 tz.setRawOffset(0); // Setting of GMT 0 offset
38 GregorianCalendar cal = new GregorianCalendar(tz);
39 SimpleDateFormat datForm = new SimpleDateFormat("yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
40
41 datForm.setCalendar(cal);
42 byte[] utcTime = null;
43
44 try {
45 utcTime = datForm.format(cal.getTime()).getBytes("ISO-8859-1");
46 } catch (Exception e) {
47 throw SMIMEException.getInstance(this, e, "constructor");
48 }
49 this.addContent(utcTime);
50 }
51
52 /***
53 * Constructs UTC time according to the submited information in Date class
54 * @param dat0 information about date and time.
55 * @exception SMIMEException thrown in super class constructor. Also, it can be
56 * caused by non SMIMEException which is: UnsupportedEncodingException.
57 */
58 public DERUTCTime(Date dat0) throws SMIMEException {
59 super(23);
60 SimpleTimeZone tz = (SimpleTimeZone) SimpleTimeZone.getDefault();
61
62 tz.setRawOffset(0); // Setting of GMT 0 offset
63 GregorianCalendar cal = new GregorianCalendar(tz);
64
65 cal.setTime(dat0);
66 SimpleDateFormat datForm = new SimpleDateFormat("yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
67
68 datForm.setCalendar(cal);
69 byte[] utcTime = null;
70
71 try {
72 utcTime = datForm.format(cal.getTime()).getBytes("ISO-8859-1");
73 } catch (Exception e) {
74 throw SMIMEException.getInstance(this, e, "constructor");
75 }
76 this.addContent(utcTime);
77 }
78
79 /***
80 * Constructs UTC time according to submited information in GregorianCalendar
81 * class.
82 * @param cal0 information about date and time.
83 * @exception SMIMEException thrown in super class constructor. Also, it can be
84 * caused by non SMIMEException which is: UnsupportedEncodingException.
85 */
86 public DERUTCTime(GregorianCalendar cal0) throws SMIMEException {
87 super(23);
88 SimpleTimeZone tz = (SimpleTimeZone) SimpleTimeZone.getDefault();
89
90 tz.setRawOffset(0); // Setting of GMT 0 offset
91 cal0.setTimeZone(tz);
92 SimpleDateFormat datForm = new SimpleDateFormat("yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
93
94 datForm.setCalendar(cal0);
95 byte[] utcTime = null;
96
97 try {
98 utcTime = datForm.format(cal0.getTime()).getBytes("ISO-8859-1");
99 } catch (Exception e) {
100 throw SMIMEException.getInstance(this, e, "constructor");
101 }
102 this.addContent(utcTime);
103 }
104
105 /***
106 * Constructs UTC time according to the definition of elements in byte array
107 * it the following form: YYMMDDhhmmssZ
108 * @param utcTime0 byte array representation of UTC elements.
109 * @exception SMIMEException thrown in super class constructor.
110 */
111 public DERUTCTime(byte[] utcTime0) throws SMIMEException {
112 super(23);
113 this.addContent(utcTime0);
114 }
115
116 /***
117 * Constructs UTC time according to the definition of elements in String
118 * it the following form: YYMMDDhhmmssZ
119 * @param utcTime0 is String representation of UTC elements.
120 * @exception SMIMEException thrown in super class constructor. Also, it can be
121 * caused by non SMIMEException which is: UnsupportedEncodingException.
122 */
123 public DERUTCTime(String utcTime0) throws SMIMEException {
124 super(23);
125 try {
126 this.addContent(utcTime0.getBytes("ISO-8859-1"));
127 } catch (Exception e) {
128 throw SMIMEException.getInstance(this, e, "constructor");
129 }
130 }
131 }
132
This page was automatically generated by Maven